#ifndef __DIALOG_H__
#define __DIALOG_H__

#include "cocos2d.h"
#include "util.h"
#include "MenuScene.h"
USING_NS_CC;

namespace suta
{

	class DialogListener
	{
	public:
		virtual void onResume()=0;
		virtual void onRestart()=0;
		virtual void onNext()=0;
	};
	/************************************************************************/
	/*                                                                      */
	/************************************************************************/
	class Dialog:public CCLayer
	{
		DialogListener*m_pListener;
	protected :
		virtual void initBtns()=0;
	public:
		Dialog():CCLayer(),m_pListener(NULL){};
		inline void setListener(DialogListener *l){m_pListener=l;};
		virtual bool init(DialogListener*listener)
		{
			bool ret=false;
			do 
			{
				CC_BREAK_IF(!CCLayer::init());
				
				m_pListener=listener;
				setContentSize(CCSizeMake(800,480));
				{
					CCSprite*bg=CCSprite::spriteWithFile("bg/bg_dialog.png");
					bg->setPosition(ccp(400,240));
					addChild(bg,-2);
				}

				initBtns();

				ret=true;
			} while (0);
			return ret;
		};


		virtual void onResumeClicked(CCObject*pSender){

			if (m_pListener)
			{
				m_pListener->onResume();
			}

		};
		virtual void onRestartClicked(CCObject*pSender){

			if (m_pListener)
			{
				m_pListener->onRestart();
			}
		};
		void onNextClicked(CCObject*pSender){
			if (m_pListener)
			{
				m_pListener->onNext();
			}
		};
		void onStopClicked(CCObject*pSender){
			CCScene*scene=MenuScene::node();
			CCDirector::sharedDirector()->replaceScene(CCTransitionSlideInT::transitionWithDuration(1.0f,scene));
		};
	};

	/************************************************************************/
	/*                                                                      */
	/************************************************************************/
	class PauseDialog:public Dialog
	{

	protected:
		void initBtns()
		{
			CCMenuItemImage*resume=CCMenuItemImage::itemFromNormalImage("menu/resume.png",NULL,this,menu_selector(PauseDialog::onResumeClicked));
			CCMenuItemImage*restart=CCMenuItemImage::itemFromNormalImage("menu/restart.png",NULL,this,menu_selector(PauseDialog::onRestartClicked));
			CCMenuItemImage*stop=CCMenuItemImage::itemFromNormalImage("menu/stop.png",NULL,this,menu_selector(PauseDialog::onStopClicked));

			CCMenu*menu=CCMenu::menuWithItems(resume,restart,stop,NULL);
			menu->alignItemsHorizontally();

			addChild(menu);
		};
	public:	
		LAYER_NODE_FUNC_PARAM(PauseDialog,DialogListener*,listener);
	};

	class EndDialog:public Dialog
	{
		bool m_bSucceed;
	protected:
		void initBtns()
		{

			CCMenuItemImage*restart=CCMenuItemImage::itemFromNormalImage("menu/restart.png",NULL,this,menu_selector(EndDialog::onRestartClicked));
			CCMenuItemImage*stop=CCMenuItemImage::itemFromNormalImage("menu/stop.png",NULL,this,menu_selector(EndDialog::onStopClicked));

			CCMenu*menu=NULL;
			if (m_bSucceed)
			{
				CCMenuItemImage*next=CCMenuItemImage::itemFromNormalImage("menu/next.png",NULL,this,menu_selector(EndDialog::onNextClicked));
				menu=CCMenu::menuWithItems(restart,stop,next,NULL);
			}else
			{
				menu=CCMenu::menuWithItems(restart,stop,NULL);
			}
			
			menu->alignItemsHorizontally();
			addChild(menu);

		};
	public:
		bool init(DialogListener*listener,bool succeed)
		{
			bool ret=false;
			do 
			{
				m_bSucceed=succeed;
				CC_BREAK_IF(!Dialog::init(listener));
				ret=true;
			} while (0);
			return ret;
		}
		LAYER_NODE_FUNC_PARAM_2(EndDialog,DialogListener*,listener,bool,succeed);

	};
	/************************************************************************/
	/*                                                                      */
	/************************************************************************/
	class FinalDialog:public Dialog
	{
	protected:
		void initBtns()
		{
			CCMenuItemImage*final=CCMenuItemImage::itemFromNormalImage("menu/final.png",NULL,this,menu_selector(EndDialog::onStopClicked));
			CCMenu*menu=CCMenu::menuWithItem(final);
			addChild(menu);
		};
	public:
		LAYER_NODE_FUNC_PARAM(FinalDialog,DialogListener*,listener);
	};

}//namespace suta

#endif